package com.zm.demo.ws.client;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;
import java.util.Map;

/**
 * Created by Administrator on 2020/2/24.
 */
public class HttpUtil {

    /**
     * 使用SOAP发送消息
     *
     * @param postUrl
     * @param mapXml
     * @param soapAction
     * @return
     */
    public static String doPostSoap(String postUrl, Map<String, Object> mapXml,
                                       String soapAction) {
        String retStr = "";
        String xml = "";
        // 创建HttpClientBuilder
        HttpClientBuilder
                httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(postUrl);
        //  设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout( 3000 )
                .setConnectTimeout( 3000 ).build();
        httpPost.setConfig(requestConfig);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8" );
            httpPost.setHeader("SOAPAction", soapAction );

            xml += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:"+ soapAction +" xmlns:ns2=\"http://server.webservice.demo.zm.com/\">";
            int i = 0;
            for ( String key : mapXml.keySet() ) {
                xml += "         <arg" + i + "><![CDATA[" + mapXml.get( key ) + "]]></arg" + i + ">\n";
                i++;
            }
            xml += "</ns2:" + soapAction + "></soap:Body></soap:Envelope>";
            System.out.println(xml);

            StringEntity data = new StringEntity( "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:getUser xmlns:ns2=\"http://server.webservice.demo.zm.com/\"><arg0><name>zm123</name><age>111</age></arg0></ns2:getUser></soap:Body></soap:Envelope>", Charset.forName("UTF-8") );
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
            }
            // 释放资源
            closeableHttpClient.close();
        } catch (Exception e) {
            System.out.println("exception in doPostSoap1_1" + e);
        }

        retStr = retStr.substring( retStr.indexOf("<return>") + 8, retStr.indexOf("</return>"));
//        retStr = StringEscapeUtils.unescapeHtml( retStr );
        return retStr;
    }

}
